package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeExpertAdviceService;
import com.bidevalution.dao.entity.BeExpertAdvice;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.mapper.BeExpertAdviceMapper;
import com.bidevalution.dao.mapper.BeSectionExpertInfoMapper;
import com.qtp.common.constants.SystemConstants;
import com.qtp.core.model.Response;
import com.qtp.core.util.OConvertUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 查慧英
 * @date 2019-11-21 13:24
 */
@Service(value = "beExpertAdviceService")
public class BeExpertAdviceServiceImpl implements BeExpertAdviceService {

    private Logger logger = Logger.getLogger(getClass());
    private static final String SECTION_ID = "sectionId";
    private static final String EXPERT_ID = "expertId";
    private static final String ENTERPRISE_NAME = "enterpriseName";
    private static final String UNPASSED_REASON = "unpassedReason";
    private static final String COLON = "：";
    private static final String COMMA = ",";
    private static final String STOP_SIGN = "、";
    @Autowired(required = false)
    BeExpertAdviceMapper beExpertAdviceMapper;

    @Autowired(required = false)
    private BeSectionExpertInfoMapper beSectionExpertInfoMapper;
    @Override
    public Response<BeExpertAdvice> getUnPassedInfo(Map<String, Object> params) {
        try {
            BeSectionExpertInfo info = getExpertInfo(params);
            String expertId = info.getSeqId();
            params.put(EXPERT_ID, expertId);
            BeExpertAdvice item = beExpertAdviceMapper.getBeExpertAdvice(params);
            if (item == null) {
                item = new BeExpertAdvice();
                List<Map<String, Object>> list = beExpertAdviceMapper.getUnPassedInfoList(params);
                if (list != null) {
                    String unPassedReason = getUnPassed(list);
                    item.setUnpassedInfo(unPassedReason);
                }
                String sectionId = OConvertUtils.getString(params.get(SECTION_ID));
                item.setSeqId(UUID.randomUUID().toString());
                item.setSectionId(sectionId);
                item.setExpertId(expertId);
                item.setCreateTime(new Date());
                beExpertAdviceMapper.create(item);
            } else {
                if (OConvertUtils.isEmpty(item.getUnpassedInfo())) {
                    List<Map<String, Object>> list = beExpertAdviceMapper.getUnPassedInfoList(params);
                    if (list != null) {
                        String unPassedReason = getUnPassed(list);
                        item.setUnpassedInfo(unPassedReason);
                        item.setCreateTime(new Date());
                        beExpertAdviceMapper.update(item);
                    }
                }
            }
            return Response.ok(item);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return Response.fail("操作失败！");
        }
    }
    private BeSectionExpertInfo getExpertInfo(Map<String,Object> parameters) {
        String loginName = OConvertUtils.getString(parameters.get("loginName"));
        String sectionId = OConvertUtils.getString(parameters.get("sectionId"));
        return beSectionExpertInfoMapper.selectByLoginName(sectionId, loginName);
    }
    private String getUnPassed(List<Map<String,Object>> list) {
        StringBuilder sb = new StringBuilder();
        int num = 1;
        String unPassedReason;
        String enterpriseName;
        String[] reasonAttr;
        for (Map<String, Object> map : list) {
            unPassedReason = OConvertUtils.getString(map.get(UNPASSED_REASON));
            enterpriseName = OConvertUtils.getString(map.get(ENTERPRISE_NAME));
            if (OConvertUtils.isNotEmpty(unPassedReason)) {
                reasonAttr = unPassedReason.split(COMMA);
                sb.append(num++).append(STOP_SIGN).append(enterpriseName).append(COLON);
                for (int i = 0; i < reasonAttr.length; i++) {
                    sb.append(reasonAttr[i]);
                }
            }
        }
        return sb.toString();
    }

    @Override
    public Response<BeExpertAdvice> getObjectionInfo(Map<String, Object> params) {
        try {
            BeSectionExpertInfo info = getExpertInfo(params);
            String expertId = info.getSeqId();
            params.put(EXPERT_ID, expertId);
            BeExpertAdvice item = beExpertAdviceMapper.getBeExpertAdvice(params);
            if (item == null) {
                String sectionId = OConvertUtils.getString(params.get(SECTION_ID));
                item = new BeExpertAdvice();
                item.setSeqId(UUID.randomUUID().toString());
                item.setSectionId(sectionId);
                item.setExpertId(expertId);
            }
            return Response.ok(item);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return Response.fail("操作失败！");
        }
    }

    @Override
    public Response addOrUpdate(BeExpertAdvice item) {
        BeExpertAdvice expertAdvice = beExpertAdviceMapper.load(item.getSeqId());
        if (expertAdvice != null) {
            item.setUpdateTime(new Date());
            return Response.ok(beExpertAdviceMapper.update(item));
        } else {
            item.setCreateTime(new Date());
            return  Response.ok(beExpertAdviceMapper.create(item));
        }
    }
}
